package com.mc.moring.widge;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.mc.moring.R;
import com.mc.moring.bean.VersionBean;

/**
 * @ProjectName: ZhiLiProject
 * @Package: com.xx.text.widget
 * @ClassName: DialogView
 * @Author: ANWEN
 * @CreateDate: 12/12/23 7:52 PM
 * @UpdateUser:
 * @UpdateDate: 12/12/23 7:52 PM
 * @UpdateRemark:
 * @Version:
 */
public class UpdateVersionDialog extends Dialog {

    DialogViewOnClickListener dialogViewOnClickListener;

    public UpdateVersionDialog(@NonNull Context context) {
        super(context, R.style.JDialog_Them);
    }

    public UpdateVersionDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected UpdateVersionDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public interface DialogViewOnClickListener{
        void onClickConfirm();
        void onClickCancel(boolean isForce);
    }

    public void setDialogViewOnClickListener(DialogViewOnClickListener dialogViewOnClickListener){
        this.dialogViewOnClickListener = dialogViewOnClickListener;
    }

    public void setMagin(Activity activity, View layoutParent, int number){
        if (layoutParent.getLayoutParams() instanceof LinearLayout.LayoutParams) {
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) layoutParent.getLayoutParams();
            lp.width = getScreenWidth(activity) - number;
            layoutParent.setLayoutParams(lp);
        }else if(layoutParent.getLayoutParams() instanceof FrameLayout.LayoutParams){
            FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) layoutParent.getLayoutParams();
            lp.width = getScreenWidth(activity) - number;
            layoutParent.setLayoutParams(lp);
        }
    }

    public static int getScreenWidth(Activity context){
        Display display = context.getWindowManager().getDefaultDisplay();	//获得默认（本地）的显示设备
        int screen_width = display.getWidth();
        return screen_width;
    }

    private boolean cancel;
//    @Override
//    public void setCanceledOnTouchOutside(boolean cancel) {
//        this.cancel = cancel;
//        super.setCanceledOnTouchOutside(cancel);
//    }

    @Override
    public void onBackPressed() {
//        if (!cancel){
//            return;
//        }
        super.onBackPressed();
    }

    public void showToUpdateDialog(Context context, VersionBean versionBean, Boolean isFocus) {
        View view = View.inflate(context,R.layout.dialog_update_version, null);
        Window window = this.getWindow();
        window.setBackgroundDrawableResource(R.color.transparent);
        TextView tv_content = view.findViewById(R.id.tv_content);
        TextView tv_cancel = view.findViewById(R.id.tv_cancel);
        TextView tv_actualizar = view.findViewById(R.id.tv_actualizar);
        TextView tv_version = view.findViewById(R.id.tv_version);
        tv_version.setText(versionBean.getVersion());
        tv_content.setText(versionBean.getMemo());

        UpdateVersionDialog myDialog = new UpdateVersionDialog(context);
        myDialog.setContentView(view);
        if (isFocus) {
            myDialog.setCanceledOnTouchOutside(false);
        } else {
            myDialog.setCanceledOnTouchOutside(true);
        }
        myDialog.show();

        tv_actualizar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(dialogViewOnClickListener!=null){
                    dialogViewOnClickListener.onClickConfirm();
                }
                myDialog.dismiss();
            }
        });
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(dialogViewOnClickListener!=null){
                    dialogViewOnClickListener.onClickCancel(versionBean.isForce());
                }
                myDialog.dismiss();
            }
        });

    }
}
